using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Srvtools;

namespace cBWhState
{
    public partial class Form1 : InfoForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        ////////////
        public string GetToday()
        {
            return string.Format("{0:yyyyMMdd}", DateTime.Today);
        }

        public string GetNowTime()
        {

            return string.Format("{0:hhmmss}", DateTime.Now);
        }

        public string GetUser()
        {
            return CliUtils.fLoginUser;
        }

        private void infoDataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //計算總價＝售價﹡庫存量,在DataGridView內編輯
            if ((inMaster.GetCurrentState() == "Inserting") || (inMaster.GetCurrentState() == "Editing"))
            {
                float AM_AMT = 0;
                if ((e.ColumnIndex == 2) || (e.ColumnIndex == 3))
                {
                    AM_AMT = float.Parse(infoDataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
                    AM_AMT = AM_AMT * float.Parse(infoDataGridView1[5 - e.ColumnIndex, e.RowIndex].Value.ToString());
                    infoDataGridView1[4, e.RowIndex].Value = AM_AMT;
                }
            }
        }

        private void ibsMaster_BeforeEdit(object sender, EventArgs e)
        {
            //在新增就顯示建檔時間 
            infoTextBox6.Text = DateTime.Now.ToLongTimeString();          
        }       
          
        private void ibsMaster_CurrentChanged(object sender, EventArgs e)
        {           
             string state = inMaster.GetCurrentState();
             if (state == "Inserting" || state == "Editing" || state == "Initial" || state == "Browsed")
             {
                 //修改時間顯示
                 string UPDATE_TIME = ((DataRowView)ibsMaster.Current)["UPDATE_TIME"].ToString();
                 if (UPDATE_TIME.Length == 6)
                     infoTextBox6.Text = Convert.ToDateTime(UPDATE_TIME.Substring(0, 2) + ":" + UPDATE_TIME.Substring(2, 2) + ":" + UPDATE_TIME.Substring(4, 2)).ToLongTimeString();
                 else if (UPDATE_TIME.Length == 0)
                     infoTextBox6.Text = "";
             }          
        }

        private void ibsMaster_AddingNew(object sender, AddingNewEventArgs e)
        {
            
        }

        private void bindingNavigatorEditItem1_Click(object sender, EventArgs e)
        {
            //以string儲存,在按下修改時  
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser.ToString();
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);

        }

        private void ibsMaster_AfterEdit(object sender, EventArgs e)
        {
            //以string儲存  
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser.ToString();
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now); 
        }

        private void infoDataGridView1_Click(object sender, EventArgs e)
        {
            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False)
            this.infoDataGridView1.ReadOnly = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //呼叫sBWhState 的MyDelete 方法，保留傳回值但不使用
            object[] back = CliUtils.CallMethod("sBWhState", "MyDelete", new object[] { 0 }); 
        }

        private void inMaster_StateChanged(object sender, InfoNavigatorStateChangedEventArgs e)
        {
            //將DateTimePicker設為不可改
            this.InfoDateTimePicker0.Enabled = false;
        }

        private void bindingNavigatorAddNewItem1_MouseUp(object sender, MouseEventArgs e)
        {
            //將DateTimePicker設為不可改
            this.InfoDateTimePicker0.Enabled = false;
        }
        

      

    }
}

